<template>
  <div id="OnlineTheory">
    <div class="OnlineHeader">
      <div class="font_header">在线理论考核</div>
    </div>
    <div class="OnlineSearch">
      <a-select
        v-model="trainingId"
        placeholder="选择训练计划"
        class="courseClass"
      >
        <a-select-option
          v-for="item in trainingIdList"
          :key="item.id"
          :value="item.id"
          >{{ item.name }}</a-select-option
        >
      </a-select>
      <a-button type="primary" style="margin-left: 10px" @click="beginClick"
        >开始考核</a-button
      >
      <a-button type="primary" style="margin-left: 10px" @click="$router.push({name:'EquipmentTrain'})">返回</a-button>
    </div>
    <div class="OnlineContainer">
      <div class="leftContainer">
        <div class="leftTitle">{{ hours }}：{{ minutes }}：{{ seconds }}</div>
        <div class="leftBox">
          <div class="leftBoxText">单选题</div>
          <div
            class="AnswerArea"
            v-for="(item, index) in singleChoiceList"
            :key="item.id"
          >
            <img src="@/assets/img/OnlineLine.png" />
            <div>
              <span>{{ item.keyIndex }}、</span><span>{{ item.topic }}</span>
            </div>
            <a-radio-group
              v-model="leftCheckList1[index].selectValue"
              @change="(value) => updateSelectValue1(item, value)"
            >
              <a-radio
                v-for="selectItem in item.optionList"
                :key="selectItem.id + '-' + selectItem.questionBankId"
                :style="radioStyle"
                :value="selectItem.id"
              >
                {{ selectItem.questionOption }}
              </a-radio>
            </a-radio-group>
          </div>

          <div class="leftBoxText" style="margin-top: 150px">多选题</div>
          <div
            class="AnswerArea"
            v-for="(item, index) in multipleChoiceList"
            :key="item.id"
          >
            <img src="@/assets/img/OnlineLine.png" />
            <div>
              <span>{{ item.keyIndex }}、</span><span>{{ item.topic }}</span>
            </div>
            <a-checkbox-group
              v-model="leftCheckList2[index].selectValue"
              :options="leftCheckList2[index].optionList"
              @change="(value) => updateSelectValue2(item, value)"
            />
          </div>

          <div class="leftBoxText" style="margin-top: 150px">判断题</div>
          <div
            class="AnswerArea"
            v-for="(item, index) in trueList"
            :key="item.id"
          >
            <img src="@/assets/img/OnlineLine.png" />
            <div>
              <span>{{ item.keyIndex }}、</span><span>{{ item.topic }}</span>
            </div>
            <a-radio-group
              v-model="leftCheckList3[index].selectValue"
              @change="(value) => updateSelectValue3(item, value)"
            >
              <a-radio
                v-for="selectItem in item.optionList"
                :key="selectItem.id + '-' + selectItem.questionBankId"
                :style="radioStyle"
                :value="selectItem.id"
              >
                {{ selectItem.questionOption }}
              </a-radio>
            </a-radio-group>
          </div>
        </div>
      </div>
      <div class="rightContainer">
        <div class="rightHeader">
          <div>单选题:{{ singleChoiceList.length }}</div>
          <div>多选题:{{ multipleChoiceList.length }}</div>
          <div>判断题:{{ trueList.length }}</div>
        </div>
        <div class="rightBoss">
          <div>
            <div class="rightBossText">单选题</div>
            <a-checkbox-group v-model="checkboxList1" class="checkboxOnline">
              <a-row>
                <a-col
                  style="height: 30px"
                  :span="8"
                  v-for="(item, index) in rightList1"
                  :key="index"
                >
                  <a-checkbox :value="item.id">
                    {{ item.label }}
                  </a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </div>

          <div style="margin-top: 30px">
            <div class="rightBossText">多选题</div>
            <a-checkbox-group v-model="checkboxList2" class="checkboxOnline">
              <a-row>
                <a-col
                  style="height: 30px"
                  :span="8"
                  v-for="(item, index) in rightList2"
                  :key="index"
                >
                  <a-checkbox :value="item.id">
                    {{ item.label }}
                  </a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </div>

          <div style="margin-top: 30px">
            <div class="rightBossText">判断题</div>
            <a-checkbox-group v-model="checkboxList3" class="checkboxOnline">
              <a-row>
                <a-col
                  style="height: 30px"
                  :span="8"
                  v-for="(item, index) in rightList3"
                  :key="index"
                >
                  <a-checkbox :value="item.id">
                    {{ item.label }}
                  </a-checkbox>
                </a-col>
              </a-row>
            </a-checkbox-group>
          </div>
        </div>
        <div class="footerBox" @click="primaryClick">确认提交</div>
      </div>
    </div>
    <a-modal
      v-model="visible"
      :maskClosable="false"
      title="成绩单"
      width="20%"
      @cancel="handleCancel"
      :footer="null"
    >
      <a-form-model
        :label-col="{ span: 8 }"
        :wrapper-col="{ span: 12 }"
        ref="formRef"
      >
        <a-form-model-item label="考核用时">
          <span style="color: white;">{{ usedTime }}</span>
        </a-form-model-item>
        <a-form-model-item label="实际成绩">
          <span style="color: white;">{{ score }}</span>
        </a-form-model-item>
        <a-form-model-item label="满分成绩">
          <span style="color: white;">{{ totalScore }}</span>
        </a-form-model-item>
        <a-form-model-item label="">
          <a-button style="margin-left: 40px" @click="$router.push({name:'home'})">跳转首页</a-button>
        </a-form-model-item>
      </a-form-model>
      <template slot="footer">
        <a-button key="back" class="modalCancel" @click="handleCancel"
          >取消</a-button
        >
        <a-button key="submit" @click="primaryClick" type="primary">
          确定
        </a-button>
      </template>
    </a-modal>
  </div>
</template>

<script>
import moment from "moment";
export default {
  data() {
    return {
      visible: false,
      usedTime: null, //用时
      score: null, //考核实际成绩
      totalScore: null, //考核满分
      assessmentTimeout: null, //倒计时结束时间,单位分钟
      assessmentId: null, //考核Id
      trainingId: "",
      trainingIdList: [],
      type: null, //1代表在线理论考核就是当前页面进来的,2代表从其他页面跳转来的
      course: "",
      value: 1,
      radioStyle: {
        display: "block",
        height: "30px",
        lineHeight: "30px",
      },
      leftCheckList1: [], //左边的答题单选题所选值的集合
      leftCheckList2: [], //左边的答题多选题所选值的集合
      leftCheckList3: [], //左边的答题判断题所选值的集合

      singleChoiceList: [], //单选题的集合
      multipleChoiceList: [], //多选题的集合
      trueList: [], //判断题的集合

      rightList1: [], //右边的单选题答题卡的集合
      rightList2: [], //右边的多选题答题卡的集合
      rightList3: [], //右边的判断题答题卡的集合

      checkboxList1: [], //右边的答题卡单选题的选中的集合
      checkboxList2: [], //右边的答题卡多选题的选中的集合
      checkboxList3: [], //右边的答题卡判断题的选中的集合

      timer: null, //倒计时计时器
      hours: "00",
      minutes: "00",
      seconds: "00",
    };
  },
  created() {
    if (this.$route.query && this.$route.query.data) {
      this.type = 2;
      this.trainingId = this.$route.query.data.id;
      this.beginClick();
    } else {
      this.type = 1;
    }
    this.getTrainingIdList(); //获取训练计划的下拉
  },
  methods: {
    handleCancel() {
      this.usedTime = null; //用时
      this.score = null; //实际成绩
      this.totalScore = null; //满分成绩
      this.visible = false;
    },
    primaryClick() {
      this.handleCancel();
    },
    getTrainingIdList() {
      this.$axios.get("/assessment/training/options").then((res) => {
        if (res.code == 200) {
          this.trainingIdList = res.data;
        }
      });
    },
    updateSelectValue1(item, value) {
      let str = "";
      switch (value.target.value) {
        case 0:
          str = "A";
          break;
        case 1:
          str = "B";
          break;
        case 2:
          str = "C";
          break;
        case 3:
          str = "D";
          break;
      }
      let data = {
        assessmentId: this.assessmentId,
        questionId: item.questionId,
        solution: str,
      };
      this.$axios.post("/assessment/submit/question", data).then((res) => {
        if (res.code == 200) {
          // this.$message.success("操作成功");
          let keyIndex = item.keyIndex * 1 - 1;
          if (this.checkboxList1.indexOf(keyIndex) == -1) {
            this.checkboxList1.push(keyIndex); //右边的答题卡的选中的集合
          }
        }
      });
    },
    updateSelectValue2(item, value) {
      const mapping = { 0: "A", 1: "B", 2: "C", 3: "D" }; // 创建一个映射对象
      const result = value.map((num) => mapping[num]).join(",");
      let data = {
        assessmentId: this.assessmentId,
        questionId: item.questionId,
        solution: result,
      };
      this.$axios.post("/assessment/submit/question", data).then((res) => {
        if (res.code == 200) {
          // this.$message.success("操作成功");
          let keyIndex = item.keyIndex * 1 - 1;
          if (this.checkboxList2.indexOf(keyIndex) == -1 && value.length > 0) {
            this.checkboxList2.push(keyIndex); //右边的答题卡的选中的集合
          }
          if (this.checkboxList2.indexOf(keyIndex) != -1 && value.length <= 0) {
            this.checkboxList2.splice(this.checkboxList2.indexOf(keyIndex), 1);
          }
        }
      });
    },
    updateSelectValue3(item, value) {
      let str = "";
      switch (value.target.value) {
        case 0:
          str = "A";
          break;
        case 1:
          str = "B";
          break;
        case 2:
          str = "C";
          break;
        case 3:
          str = "D";
          break;
      }
      let data = {
        assessmentId: this.assessmentId,
        questionId: item.questionId,
        solution: str,
      };
      this.$axios.post("/assessment/submit/question", data).then((res) => {
        if (res.code == 200) {
          // this.$message.success("操作成功");
          let keyIndex = item.keyIndex * 1 - 1;
          if (this.checkboxList3.indexOf(keyIndex) == -1) {
            this.checkboxList3.push(keyIndex); //右边的答题卡的选中的集合
          }
        }
      });
    },
    beginClick() {
      //开始考核
      clearInterval(this.timer);
      this.timer = null;
      this.$axios
        .post("/assessment/start", {
          type: this.type,
          trainingId: this.trainingId,
        })
        .then((res) => {
          this.rightList1 = []; //单选题答题卡
          this.checkboxList1 = []; //单选题答题卡选中的集合
          this.leftCheckList1 = []; //左边的答题卡单选所选值的集合
          this.assessmentId = res.data.assessmentId;
          this.assessmentTimeout = res.data.assessmentTimeout; //倒计时结束时间,单位分钟
          this.getTimer(); //计时器
          res.data.questions.sort((a, b) => a.type - b.type);
          res.data.questions.forEach((item, index) => {
            item.selectValue = 0; //选项选中的值
            item.optionList = [
              {
                id: 0,
                questionOption: item.optionA,
              },
              {
                id: 1,
                questionOption: item.optionB,
              },
              {
                id: 2,
                questionOption: item.optionC,
              },
              {
                id: 3,
                questionOption: item.optionD,
              },
            ];
          });
          this.singleChoiceList = res.data.questions.filter(
            (item) => item.type == 1
          ); //单选题的集合
          this.singleChoiceList.forEach((item, index) => {
            item.keyIndex = index * 1 + 1;
            this.leftCheckList1.push({
              selectValue: null,
              keyIndex: index + 1,
            }); //左边的单选所选值的集合
            this.rightList1.push({
              label: index + 1,
              id: index,
            });
          }); //单选题的集合

          this.multipleChoiceList = res.data.questions.filter(
            (item) => item.type == 2
          ); //多选题的集合
          this.multipleChoiceList.forEach((item, index) => {
            item.keyIndex = index * 1 + 1;
            this.leftCheckList2.push({
              selectValue: null,
              optionList: item.optionList.map((itemBox) => ({
                label: itemBox.questionOption,
                value: itemBox.id,
              })),
            });
            this.rightList2.push({
              label: index + 1,
              id: index,
            });
          });

          this.trueList = res.data.questions.filter((item) => item.type == 3); //判断题的集合
          this.trueList.forEach((item, index) => {
            item.keyIndex = index * 1 + 1;
            item.optionList = item.optionList.filter(
              (item) => item.questionOption != null
            );
          }); //判断题的集合
          this.trueList.forEach((item, index) => {
            this.leftCheckList3.push({
              selectValue: null,
              keyIndex: index + 1,
            }); //左边的单选所选值的集合
            this.rightList3.push({
              label: index + 1,
              id: index,
            });
          }); //判断题的集合
        });
    },
    getTimer() {
      let totalSeconds = this.assessmentTimeout * 60; // 分钟转换为秒
      const hours = Math.floor(totalSeconds / 3600); // 获取小时数
      const minutes = Math.floor((totalSeconds % 3600) / 60); // 获取分钟数
      const seconds = totalSeconds % 60; // 获取秒数
      this.hours = String(hours).padStart(2, "0");
      this.minutes = String(minutes).padStart(2, "0");
      this.seconds = String(seconds).padStart(2, "0");
      let that = this;
      this.timer = setInterval(() => {
        const hours = Math.floor(totalSeconds / 3600); // 获取小时数
        const minutes = Math.floor((totalSeconds % 3600) / 60); // 获取分钟数
        const seconds = totalSeconds % 60; // 获取秒数
        that.hours = String(hours).padStart(2, "0");
        that.minutes = String(minutes).padStart(2, "0");
        that.seconds = String(seconds).padStart(2, "0");
        const formattedTime = `${String(hours).padStart(2, "0")}:${String(
          minutes
        ).padStart(2, "0")}:${String(seconds).padStart(2, "0")}`;
        totalSeconds--;
        if (totalSeconds < 0) {
          clearInterval(that.timer);
          this.timer = null;
        }
      }, 1000);
    },
    async primaryClick() {
      let data = {
        assessmentId: this.assessmentId,
      };
      this.$axios.post("/assessment/finish", data).then((res) => {
        if (res.code == 200) {
          this.$message.success("提交成功");
          this.usedTime = res.data.usedTime; //用时
          this.score = res.data.score; //实际成绩
          this.totalScore = res.data.totalScore; //满分成绩
          this.visible = true;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
#OnlineTheory {
  height: 100vh;
  overflow: hidden;
  background-image: url("~@/assets/img/OnlineBg.png");
  background-repeat: no-repeat;
  background-size: cover;
  .OnlineHeader {
    text-align: center;
    height: 70px;
    line-height: 70px;
    font-size: 40px;
    font-weight: 600;
    font-family: "KaiTi", "楷体", serif;
    background: #112b43;
    .font_header {
      letter-spacing: 20px;
      background: linear-gradient(to bottom, #0c84ab, #ceecf4);
      -webkit-background-clip: text; /* 只在文字上显示背景 */
      -webkit-text-fill-color: transparent; /* 填充透明色，使背景渐变显示出来 */
    }
  }
  .OnlineSearch {
    margin-top: 20px;
    margin-left: 50px;
  }
  .OnlineContainer {
    width: 100%;
    display: flex;
    height: 820px;
    justify-content: space-around;
    .leftContainer {
      width: 1180px;
      height: 100%;
      position: relative;
      background-image: url("~@/assets/img/Theory.png");
      background-repeat: no-repeat;
      background-size: cover;
      .leftTitle {
        position: absolute;
        left: 46%;
        font-size: 20px;
        color: #02d9fd;
      }
      .leftBox {
        width: 100%;
        margin-top: 3%;
        height: 94%;
        overflow-y: auto;
        .leftBoxText {
          margin-left: 30px;
          color: #02d9fd;
          font-size: 16px;
          font-family: "KaiTi", "楷体", serif;
        }
        .AnswerArea {
          padding: 0 20px 0 20px;
          font-size: 20px;
          color: white;
          font-family: "KaiTi", "楷体", serif;
        }
      }
    }
    .rightContainer {
      width: 540px;
      margin-top: 10px;
      position: relative;
      background: url("~@/assets/img/Theory2.png") no-repeat 100%;
      .rightHeader {
        position: absolute;
        left: 8%;
        top: 5%;
        display: flex;
        div {
          cursor: pointer;
          width: 150px;
          height: 50px;
          text-align: center;
          line-height: 50px;
          background: #001725;
          color: #02d9fd;
          border: 1px solid #1c44a9;
          font-size: 16px;
          font-family: "KaiTi", "楷体", serif;
        }
        .rightActived {
          background: #254989;
        }
      }
      .rightBoss {
        height: calc(100% - 250px);
        margin-top: 150px;
        overflow-y: auto;
        .rightBossText {
          margin-left: 28px;
          color: #02d9fd;
          font-size: 16px;
          font-family: "KaiTi", "楷体", serif;
        }
      }
      .checkboxOnline {
        width: 80%;
        // height: 550px;
        overflow-y: auto;
        display: block;
        margin-left: 30px;
        // position: absolute;
        // left: 10%;
        // top: 18%;
      }
      .footerBox {
        width: 300px;
        height: 50px;
        line-height: 50px;
        padding-left: 30px;
        text-align: center;
        position: absolute;
        bottom: 20px;
        left: 20%;
        background: url("~@/assets/img/primary_1.png") no-repeat 100%;
        cursor: pointer;
        color: #02d9fd;
        font-size: 16px;
        font-family: "KaiTi", "楷体", serif;
      }
    }
  }
}
</style>
<style lang="scss">
#OnlineTheory {
  .courseClass {
    width: 200px;
    .ant-select-selection {
      background: #04296d;
    }
    i {
      color: white;
    }
    .ant-select-selection-selected-value {
      color: white;
      font-family: "KaiTi", "楷体", serif;
    }
  }
  .ant-radio-wrapper {
    color: white;
  }
  .ant-checkbox-group {
    display: flex;
    flex-direction: column;
    .ant-checkbox-group-item {
      margin-top: 5px;
    }
  }
  .ant-checkbox-wrapper {
    span {
      color: white;
    }
    .ant-checkbox-disabled .ant-checkbox-inner {
      // background-color: #1890ff;
      // border-color: #1890ff !important;
    }
  }
}
</style>
